<?php

App::uses('AppController', 'Controller');

class HolidaysController extends AppController {

	public $uses = array('Holiday');

	public function index() {
		$this->set('holidays', $this->Holiday->find('all'));
	}

	/**
	 * CREATE
	 */
	public function create() {
		$this->set('data', $this->Holiday->create());
		$this->render('form');
	}
	
	/**
	 * READ
	 */
	public function show($id) {
		$this->set('data', $this->Holiday->findById($id));
	}
	
	/**
	 * EDIT (UPDATE)
	 */
	public function edit($id) {
		$this->set('data', $this->Holiday->findById($id));
		$this->render('form');
	}
	
	/**
	 * SAVE (UPDATE)
	 */
	public function save() {
		if ($this->request->is('post')) {
			$data = $this->data;
			$this->Holiday->create($data);
			
			if (! $this->Holiday->validates()) {
				$this->set('data', $data);
				$this->setFlash('Invalid data!');
				return $this->render('form');
			}
			
			if (! $this->Holiday->saveAll($data)) {
				$this->set('data', $data);
				$this->setFlash('Error saving data.');
				return $this->render('form');
			}
			
			$this->setFlash('Saved.', 'ok');
			$this->redirect('edit/' . $this->Holiday->id);
		}
		
		$this->redirect('index');
	}
	
	/**
	 * DELETE
	 */
	public function delete($id) {
		if (! $this->Holiday->delete($id)) {
			$this->setFlash('Error deleting Holiday!');
		} else {
			$this->setFlash('Holiday deleted', 'ok');
		}
		
		$this->redirect('index');
	}

}
